package com.mediacloud.app.newsmodule.activity.military_encyclopedia

import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.newsmodule.R
import com.scwang.smartrefresh.layout.util.SmartUtil

class MilitaryEncyclopediaTopicRelationAdapter : BaseQuickAdapter<RelativeCatalogList,BaseViewHolder>(R.layout.item_bk_relation_topic) {
    override fun convert(helper: BaseViewHolder, item:RelativeCatalogList) {
        val ivImage = helper.getView<ImageView>(R.id.ivImage)
        val ivLogo1 = helper.getView<ImageView>(R.id.ivLogo1)
        val ivLogo2 = helper.getView<ImageView>(R.id.ivLogo2)
        val ivLogo3 = helper.getView<ImageView>(R.id.ivLogo3)
        val itemLayout = helper.getView<ConstraintLayout>(R.id.itemLayout)
        val tvMore = helper.getView<TextView>(R.id.tvMore)
        val layoutContent3 = helper.getView<ConstraintLayout>(R.id.layoutContent3)
        val layoutContent2 = helper.getView<ConstraintLayout>(R.id.layoutContent2)
        val layoutContent1 = helper.getView<ConstraintLayout>(R.id.layoutContent1)
        val layoutParams = itemLayout.layoutParams as RecyclerView.LayoutParams
        if (itemCount == 1){
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
            layoutParams.marginEnd = SmartUtil.dp2px(8f)
        }else{
            layoutParams.width = SmartUtil.dp2px(300f)
            layoutParams.marginEnd = SmartUtil.dp2px(8f)
        }
        itemLayout.layoutParams = layoutParams
        helper.addOnClickListener(R.id.tvMore)
        helper.addOnClickListener(R.id.layoutContent3)
        helper.addOnClickListener(R.id.layoutContent2)
        helper.addOnClickListener(R.id.layoutContent1)

        ivImage.loadX(item.logo,3,1,false,null)
        item.articles?.let{
           if (it.isNotEmpty()){
               when(it.size){
                   1->{
                       helper.setText(R.id.tvTitle1,it[0].title)
                       helper.setText(R.id.tvMeaings1,it[0].shortTitle)
                       ivLogo1.loadX(it[0].logo,1,1,false,null)
                   }
                   2->{
                       helper.setText(R.id.tvTitle1,it[0].title)
                       helper.setText(R.id.tvMeaings1,it[0].shortTitle)
                       ivLogo1.loadX(it[0].logo,1,1,false,null)

                       helper.setText(R.id.tvTitle2,it[1].title)
                       helper.setText(R.id.tvMeaings2,it[1].shortTitle)
                       ivLogo2.loadX(it[1].logo,1,1,false,null)
                   }
                   else->{
                       helper.setText(R.id.tvTitle1,it[0].title)
                       helper.setText(R.id.tvMeaings1,it[0].shortTitle)
                       ivLogo1.loadX(it[0].logo,1,1,false,null)

                       helper.setText(R.id.tvTitle2,it[1].title)
                       helper.setText(R.id.tvMeaings2,it[1].shortTitle)
                       ivLogo2.loadX(it[1].logo,1,1,false,null)

                       helper.setText(R.id.tvTitle3,it[2].title)
                       helper.setText(R.id.tvMeaings3,it[2].shortTitle)
                       ivLogo3.loadX(it[2].logo,1,1,false,null)

                   }
               }
           }


        }

    }

}